package com.kaisheng.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.kaisheng.entity.Idea;
import com.kaisheng.entity.IdeaComment;
import com.kaisheng.entity.User;
import com.kaisheng.util.DBHelp;
import com.kaisheng.util.RowMapper;

public class IdeaCommentDao {

	private DBHelp<IdeaComment> db = new DBHelp<IdeaComment>();
	

	public List<IdeaComment> findAllCommentsById(String ideaid) {
	
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT ic.id,ic.content AS commentcontent,ic.createtime AS commentcreatetime,ic.user_id ,u.name AS username,u.password,u.email,u.pic ,ic.idea_id, i.`title`,i.content AS ideacontent,i.createtime AS ideacreatetime,i.user_id,i.project_id FROM t_ideacomment AS ic ");
		sql.append(" LEFT JOIN t_idea AS i ON ic.idea_id = i.id ");
		sql.append(" LEFT JOIN t_user AS u ON ic.user_id = u.id ");
		sql.append(" WHERE ic.idea_id = ? ORDER BY ic.createtime DESC  ");
		return db.executeQueryForList(sql.toString(), new IdeaCommentRowMapper(), ideaid);
		
	}

	private class IdeaCommentRowMapper implements RowMapper<IdeaComment>{

		public IdeaComment mapRow(ResultSet rs) throws SQLException {
			
			IdeaComment ic = new IdeaComment();
			ic.setId(rs.getString("id"));
			ic.setContent(rs.getString("commentcontent"));
			ic.setCreatetime(rs.getString("commentcreatetime"));
			
			User u = new User();
		//gaile
			u.setId(rs.getString("user_id"));
			u.setName(rs.getString("username"));
			u.setPassword(rs.getString("password"));
			u.setEmail(rs.getString("email"));
			u.setPic(rs.getString("pic"));
			
			ic.setUser(u);
			ic.setUser_id(rs.getString("user_id"));
			
			Idea i = new Idea();
		//gaile
			i.setId(rs.getString("idea_id"));
			i.setTitle(rs.getString("title"));
			i.setContent(rs.getString("ideacontent"));
			i.setCreatetime(rs.getString("ideacreatetime"));
			i.setUser_id(rs.getString("user_id"));
			i.setProject_id(rs.getString("project_id"));
			
			ic.setIdea(i);
			ic.setIdea_id(rs.getString("idea_id"));
			
		
			
			
			
			return ic;
		}
		
		
		
	}

	public void saveNewComment(IdeaComment ic) {

		String sql = "INSERT INTO t_ideacomment (id,content,createtime,idea_id,user_id) VALUES (?,?,?,?,?)";
		db.executeSQL(sql, ic.getId(),ic.getContent(),ic.getCreatetime(),ic.getIdea_id(),ic.getUser_id());
	}

	public void deleteCommentById(String commentid) {
		String sql = "DELETE FROM t_ideacomment WHERE id = ? ";
		db.executeSQL(sql, commentid);
	}

	public IdeaComment findCommentsById(String commentid) {
		StringBuilder sql = new StringBuilder();
		sql.append("  SELECT ic.id,ic.content AS commentcontent,ic.createtime AS commentcreatetime,ic.user_id ,u.name AS username,u.password,u.email,u.pic ,ic.idea_id, i.`title`,i.content AS ideacontent,i.createtime AS ideacreatetime,i.user_id,i.project_id FROM t_ideacomment AS ic  ");
		sql.append("  LEFT JOIN t_idea AS i ON ic.idea_id = i.id ");
		sql.append("  LEFT JOIN t_user AS u ON ic.user_id = u.id  ");
		sql.append("  WHERE ic.id =  ? ");
		return db.executeQueryForObject(sql.toString(), new IdeaCommentRowMapper(), commentid);
	}

	public void updateById(String editcontent, String commentid) {
		String sql = " UPDATE t_ideacomment SET content= ? WHERE id = ?";
		db.executeSQL(sql,editcontent, commentid);
	}

	
	
}
